﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Manager;
using SdmxMl.Common;
using SdmxMl.Structure;


namespace SdmxMl.Fact_Dim
{
    /// <summary>
    /// This class group a codelist or a hierarchical codelist with a codelist
    /// holding the added quality codes (negativ values)
    /// </summary>
    public class NomenWithQuality
    {
        #region Internal Members

        public  CodeListType Clt { get; set; }

        public CodeListType CltQuality { get; set; }
        public HierarchicalCodelistType Hclt { get; set; }

        private BaseDimensionType bdt;

        
        #endregion

        #region Properties

        public string ConceptId { get { return bdt.ConceptInfo.Id; } }

        public BaseDimensionType Bdt { get { return bdt; } }

        public string KeyId
        {
            get
            {
                string key = string.Empty;

                if (IsEmpty == false)
                {
                    StringBuilder sb = new StringBuilder();
                    if (Clt != null)
                        sb.Append(Clt.FullIdent);

                    if (Hclt != null)
                    {
                        if (sb.Length > 0)
                            sb.Append('+');
                        sb.Append(Hclt.FullIdent);
                    }

                    if (CltQuality != null)
                    {
                        if (sb.Length > 0)
                            sb.Append('+');
                        sb.Append(CltQuality.FullIdent);
                    }

                    key = sb.ToString();
                }
                return key;
            }
        }

        public bool IsEmpty
        {
            get
            {
                return Clt == null && CltQuality == null && Hclt == null;
            }
        }

        public bool IsHierarchicalCodelist
        {
            get
            {
                return Hclt != null;
            }
        }

        #endregion

        #region C'tor

        public NomenWithQuality(BaseDimensionType bVar)
        {
            bdt = bVar;
        }

        #endregion
    }

    /// <summary>
    /// Reference helper to get concept, codelists or hierarchy
    /// </summary>
    public class RefNomen : RefVarHelper
    {

       #region construction

        public RefNomen() : base()
        {
        }

        public RefNomen(SmManager sm) : base(sm)
        {
        }

        #endregion

        public List<NomenWithQuality> GetListOfNomen(KeyFamilyType dsd)
        {
            List<NomenWithQuality> listClt = new List<NomenWithQuality>(20);
            List<BaseDimensionType> lbd = PrepareVarList(dsd, false);

            foreach (BaseDimensionType bdt in lbd)
            {
                NomenWithQuality nmq = new NomenWithQuality(bdt);

                AnnotationList anl = bdt.AnnotationList.GetAllAnnotationOfType(AnnotHierarchyType, null);

                // Do we have a refered hierarchy
                if (anl.Count > 0)
                {
                    if (anl.Count > 1)
                        throw new ApplicationException("Multiple hierarchy ref are not yet supported!");
                    nmq.Hclt = GetHierarchyRef(anl[0], nmq.ConceptId);
                }
                else
                {
                    nmq.Clt = GetCodelistOfBaseDim(bdt);
                }

                listClt.Add(nmq);
            }
            return listClt;
        }

    }
}
